package com.zhangwei.liwushuo.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zhangwei.liwushuo.BaseApp;
import com.zhangwei.liwushuo.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;

/**
 * @说 明：
 * @项目名称：LiWuShuo
 * @包 名： com.zhangwei.liwushuo.view
 * @类 名： TitleView
 * @创 建人： zhangwei
 * @创建时间：2015-08-17 10:54
 * @版 本：v1.0
 * @修 改人：
 * @修间：
 * @修改备注：
 */
@EViewGroup(R.layout.titlebar_layout)
public class TitleBarView extends LinearLayout implements RadioGroup.OnCheckedChangeListener {
    @App
    BaseApp app;
    @ViewById
    RadioGroup titlebarRgs;
    @ViewById
    ImageButton titlebarLeft;
    @ViewById
    TextView titlebarTitle;
    @ViewById
    ImageButton titlebarRight;

    @ColorRes(R.color.white)
    int whiteColor;

    private TbCheckedChanged tbCheckedChanged;


    private TitleBarClickListener titleBarListener;

    private int defaultTextSize = 15;

    private Drawable leftDrawable;

    private Drawable rightDrawable;


    public TitleBarView(Context context) {
        super(context);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @AfterViews
    public void init() {
        titlebarTitle.setTextSize(defaultTextSize);
        titlebarRgs.setOnCheckedChangeListener(this);
        ((RadioButton) titlebarRgs.getChildAt(0)).setChecked(true);
        leftDrawable = getResources().getDrawable(R.mipmap.ic_toolkit_zebra_scanner);
        rightDrawable = getResources().getDrawable(R.mipmap.ic_menu_search);
    }

    /**
     * 显示首页
     */
    public void showHomeTitle(String title) {
        titlebarRgs.setVisibility(View.GONE);
        titlebarLeft.setVisibility(View.VISIBLE);
        titlebarLeft.setImageDrawable(leftDrawable);

        titlebarTitle.setVisibility(View.VISIBLE);
        titlebarTitle.setText(title);

        titlebarRight.setVisibility(View.VISIBLE);
        titlebarRight.setImageDrawable(rightDrawable);
    }

    /**
     * 显示单品
     */
    public void showDanPinTitle(String title) {
        titlebarRgs.setVisibility(View.GONE);

        titlebarLeft.setVisibility(View.INVISIBLE);

        titlebarTitle.setVisibility(View.VISIBLE);
        titlebarTitle.setText(title);

        titlebarRight.setVisibility(View.INVISIBLE);
    }

    /**
     * 显示分类
     */
    public void showCategoryTitle() {


        titlebarLeft.setVisibility(View.INVISIBLE);

        titlebarRgs.setVisibility(View.VISIBLE);

        titlebarTitle.setVisibility(GONE);

        titlebarRight.setVisibility(View.VISIBLE);
        titlebarRight.setImageDrawable(rightDrawable);
    }

    /**
     * 显示我的
     *
     * @param title
     */
    public void showMeTitle(String title) {
        titlebarRgs.setVisibility(View.GONE);
        titlebarLeft.setVisibility(View.INVISIBLE);

        titlebarTitle.setVisibility(View.VISIBLE);
        titlebarTitle.setText(title);

        titlebarRight.setVisibility(View.VISIBLE);
    }

    /**
     * 点击按钮监听
     */
    public interface TitleBarClickListener {
        void onTitleBarLinstener(View view);
    }

    /**
     * 分类radiobutton监听
     */
    public void setTitleBarListener(TitleBarClickListener titleBarListener) {
        this.titleBarListener = titleBarListener;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (tbCheckedChanged != null) {
            tbCheckedChanged.onTbCheckedChanged(group, checkedId);
        }
    }

    @Click({R.id.titlebar_left, R.id.titlebar_right, R.id.titlebar_title})
    public void onClickListener(View view) {
        if (titleBarListener != null) {
            titleBarListener.onTitleBarLinstener(view);
        }
    }

    public interface TbCheckedChanged {
        void onTbCheckedChanged(RadioGroup group, int checkedId);
    }

    public TbCheckedChanged getTbCheckedChanged() {
        return tbCheckedChanged;
    }

    public void setTbCheckedChanged(TbCheckedChanged tbCheckedChanged) {
        this.tbCheckedChanged = tbCheckedChanged;
    }

    public int getWhiteColor() {
        return whiteColor;
    }

    public void setWhiteColor(int whiteColor) {
        this.whiteColor = whiteColor;
    }

    public int getDefaultTextSize() {
        return defaultTextSize;
    }

    public void setDefaultTextSize(int defaultTextSize) {
        this.defaultTextSize = defaultTextSize;
    }

    public Drawable getLeftDrawable() {
        return leftDrawable;
    }

    public void setLeftDrawable(Drawable leftDrawable) {
        this.leftDrawable = leftDrawable;
    }

    public Drawable getRightDrawable() {
        return rightDrawable;
    }

    public void setRightDrawable(Drawable rightDrawable) {
        this.rightDrawable = rightDrawable;
    }
}
